const { getbander, addbanderss, updatabnader, delbanders } = require('../../service/main/mainbander')
module.exports = {
    async getbander(ctx, next) {
        return ctx.body = {
            code: 200,
            data: await getbander()
        }
    }
    , async addbander(ctx, next) {
        const data = {
            name: ctx.request.body.name,
            path: ctx.request.body.path,
            img: '/img/' + ctx.request.files.img.newFilename
        }
        try {
            await addbanderss(data)
            return ctx.body = {
                code: 200,
                msg: '添加成功'
            }
        } catch (error) {
            return ctx.body = {
                code: 500,
                msg: '添加失败'
            }
        }

    },
    async updatabander(ctx, next) {
        let data
        if (ctx.request.files.img) {
            data = {
                ...ctx.request.body,
                img: '/img/' + ctx.request.files.img.newFilename
            }
        } else {
            data = {
                ...ctx.request.body,
            }
        }
        await updatabnader(data)
        return ctx.body = {
            code: 200,
            msg: '修改成功'
        }
    },
    async delbander(ctx, next) {
        await delbanders(ctx.query.id)
        return ctx.body = {
            code: 200,
            msg: '删除成功'
        }
    }
}
